package mahi.thread;

public class SynchronizedCounter {
    private int c = 0;

    public synchronized void increment() {
        c++;
    }

    public synchronized void decrement() {
        c--;
    }

    public synchronized int value() {
        return c;
    }
    
    
    public static void main(String[] args) {
    	class MyThread1 extends Thread{
    		
    		SynchronizedCounter synchronizedCounter;
    		
    		public MyThread1(SynchronizedCounter synchronizedCounter) {
    			this.synchronizedCounter = synchronizedCounter;
			}
    		
    		@Override
    		public void run() {
    			super.run();
    			synchronizedCounter.increment();
    			System.out.println("In my thread 1 :"+ synchronizedCounter.value());
    		}
    	}
    	
    	
    	class MyThread2 extends Thread{
    		
    		SynchronizedCounter synchronizedCounter;
    		
    		public MyThread2(SynchronizedCounter synchronizedCounter) {
    			this.synchronizedCounter = synchronizedCounter;
			}
    		
    		@Override
    		public void run() {
    			super.run();
    			synchronizedCounter.decrement();
    			System.out.println("In my thread 2 :"+ synchronizedCounter.value());
    		}
    	}
    	
    	
    	
    	SynchronizedCounter synchronizedCounter = new SynchronizedCounter();
    	
    	MyThread1 myThread1 = new MyThread1(synchronizedCounter);
    	MyThread1 t2 = new MyThread1(synchronizedCounter);
    	
    	
    	SynchronizedCounter synchronizedCounterAnother = new SynchronizedCounter();
    	MyThread1 t3 = new MyThread1(synchronizedCounterAnother);
    	
    	myThread1.start();
    	t2.start();
    	t3.start();
    	
    	
    	
    	
    	MyThread2 myThread2 = new MyThread2(synchronizedCounter);
    	
    	
    	
    	myThread2.start();
    	
    	
    	
    	//Different object locks and static locks
    	
    	
    	
	}
    
}